Skip to content

TableToListProxyModel

Base classes

Name Children Inherits
FlatProxyModel
prettyqt.itemmodels.proxies.flatproxymodel
Base class for proxies which reshape a source table into another flat table.

â‹” Inheritance diagram

graph TD
  2267331299["itemmodels.TableToListProxyModel"]
  1752756326["itemmodels.FlatProxyModel"]
  445963150["itemmodels.FlatProxyMixin"]
  2751680898["builtins.object"]
  1274898529["core.AbstractProxyModel"]
  2431222121["core.AbstractProxyModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  3989152870["QtCore.QAbstractProxyModel"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  1752756326 --> 2267331299
  445963150 --> 1752756326
  2751680898 --> 445963150
  1274898529 --> 1752756326
  2431222121 --> 1274898529
  3882849576 --> 2431222121
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  3989152870 --> 1274898529
  1898060022 --> 3989152870
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: FlatProxyModel

Proxy model to flatten a table to a list.

Reshapes a table by concatenating all columns into one large column, so that the new rowCount equals to sourceModel rowCount * sourceModel columnCount. If a verticalHeader is available, it will show the original position of the cell.

data = dict(
    first=["John", "Mary"],
    last=["Doe", "Bo"],
    height=[5.5, 6.0],
    weight=[130, 150],
)
model = gui.StandardItemModel.from_dict(data)
table = widgets.TableView()
table.set_model(model)
# table.proxifier.to_list()
table.show()
Image title

data = dict(
    first=["John", "Mary"],
    last=["Doe", "Bo"],
    height=[5.5, 6.0],
    weight=[130, 150],
)
model = gui.StandardItemModel.from_dict(data)
table = widgets.TableView()
table.set_model(model)
table.proxifier.to_list()
table.show()
Image title

Source code in prettyqt/itemmodels/proxies/tabletolistproxymodel.py
class TableToListProxyModel(FlatProxyModel):
    """Proxy model to flatten a table to a list.

    Reshapes a table by concatenating all columns into one large column,
    so that the new rowCount equals to sourceModel rowCount * sourceModel columnCount.
    If a verticalHeader is available, it will show the original position of the cell.

    === "Without proxy"

        ```py
        data = dict(
            first=["John", "Mary"],
            last=["Doe", "Bo"],
            height=[5.5, 6.0],
            weight=[130, 150],
        )
        model = gui.StandardItemModel.from_dict(data)
        table = widgets.TableView()
        table.set_model(model)
        # table.proxifier.to_list()
        table.show()
        ```
        <figure markdown>
          ![Image title](../../images/tabletolistproxymodel_before.png)
        </figure>

    === "With proxy"

        ```py
        data = dict(
            first=["John", "Mary"],
            last=["Doe", "Bo"],
            height=[5.5, 6.0],
            weight=[130, 150],
        )
        model = gui.StandardItemModel.from_dict(data)
        table = widgets.TableView()
        table.set_model(model)
        table.proxifier.to_list()
        table.show()
        ```
        <figure markdown>
          ![Image title](../../images/tabletolistproxymodel_after.png)
        </figure>

    """

    ID = "table_to_list"
    ICON = "mdi6.table-pivot"

    def __init__(self, *args, header_title: str = "", **kwargs):
        self._header_title = header_title
        super().__init__(*args, **kwargs)

    def columnCount(self, parent: core.ModelIndex | None = None) -> int:
        parent = parent or core.ModelIndex()
        return 0 if self.sourceModel() is None or parent.isValid() else 1

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ) -> str | None:
        source = self.sourceModel()
        match orientation, role:
            case constants.HORIZONTAL, constants.DISPLAY_ROLE:
                return self._header_title or None
            case constants.VERTICAL, constants.DISPLAY_ROLE if source is not None:
                colcount = source.columnCount()
                if colcount == 0:
                    return None
                row_section, col_section = divmod(section, colcount)
                pre = source.headerData(col_section, constants.HORIZONTAL, role)
                post = source.headerData(row_section, constants.VERTICAL, role)
                pre_str = col_section if pre is None else pre
                post_str = row_section if post is None else post
                return f"{pre_str} | {post_str}"
        return None

    def rowCount(self, parent: core.ModelIndex | None = None) -> int:
        parent = parent or core.ModelIndex()
        source = self.sourceModel()
        if source is None or parent.isValid():
            return 0
        return source.rowCount() * source.columnCount()

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        return self.mapToSource(index).data(role) if index.isValid() else None

    def setData(
        self,
        index: core.ModelIndex,
        value,
        role: constants.ItemDataRole = constants.EDIT_ROLE,
    ) -> bool:
        source_index = self.mapToSource(index)
        if not source_index.isValid():
            return False
        return self.sourceModel().setData(source_index, value, role)

    def flags(self, index: core.ModelIndex) -> constants.ItemFlag:
        source_index = self.mapToSource(index)
        if not source_index.isValid():
            return super().flags(index)
        return self.sourceModel().flags(source_index)

    def mapToSource(self, proxy_idx: core.ModelIndex) -> core.ModelIndex:
        source = self.sourceModel()
        if source is None or not proxy_idx.isValid() or source.columnCount() == 0:
            return core.ModelIndex()
        row, col = divmod(proxy_idx.row(), source.columnCount())
        return source.index(row, col)

    def mapFromSource(self, source_index: core.ModelIndex) -> core.ModelIndex:
        source = self.sourceModel()
        if (
            source is None
            or not source_index.isValid()
            or source_index.parent().isValid()
        ):
            return core.ModelIndex()
        r = source_index.row() * source.columnCount() + source_index.column()
        return self.index(r, 0)

    def set_header_title(self, title: str):
        self._header_title = title
        self.headerDataChanged.emit(constants.HORIZONTAL, 0, 0)

    def get_header_title(self) -> str:
        return self._header_title

    header_title = core.Property(
        str,
        get_header_title,
        set_header_title,
        doc="Column header for resulting column",
    )

:fa-brands-github: Show source on GitHub